#!/bin/bash

# Slackware build script for inform

# Written by B. Watson (yalhcru@gmail.com)

# Licensed under the WTFPL. See http://www.wtfpl.net/txt/copying/ for details.

# 20210831 bkw:
# - updated for v6.35_r2 (upstream changed their version numbering
#   scheme again). script had to change a lot, so can't build old
#   versions any more.

# 20201104 bkw: updated for 6.34_6.12.4. In case anyone's wondering
# why I use a self-hosted URL for the source, it's because upstream
# moves the old source immediately when a new release is made. Also
# moved the binary to /usr/games and the man page to section 6.

# 20191216 bkw: updated for 6.34_6.12.2. The secondary version number
# is the inform6 library version. This script can no longer build the
# old 6.32.1 version, but it should be fine for future releases.

# The old 6.32.1 release shipped with a lot more include
# files. Upstream removed a lot of them because they were either buggy
# or license-incompatible. If you're trying to compile something that
# needs any of these old includes, please let me know. If there's enough
# demand, I'll dig up the old includes and re-add them to the package.

cd $(dirname $0) ; CWD=$(pwd)

PRGNAM=inform
VERSION=${VERSION:-6.35_r2}
BUILD=${BUILD:-1}
TAG=${TAG:-_SBo}
PKGTYPE=${PKGTYPE:-tgz}

if [ -z "$ARCH" ]; then
  case "$( uname -m )" in
    i?86) ARCH=i586 ;;
    arm*) ARCH=arm ;;
       *) ARCH=$( uname -m ) ;;
  esac
fi

if [ ! -z "${PRINT_PACKAGE_NAME}" ]; then
  echo "$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE"
  exit 0
fi

TMP=${TMP:-/tmp/SBo}
PKG=$TMP/package-$PRGNAM
OUTPUT=${OUTPUT:-/tmp}

if [ "$ARCH" = "i586" ]; then
  SLKCFLAGS="-O2 -march=i586 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "i686" ]; then
  SLKCFLAGS="-O2 -march=i686 -mtune=i686"
  LIBDIRSUFFIX=""
elif [ "$ARCH" = "x86_64" ]; then
  SLKCFLAGS="-O2 -fPIC"
  LIBDIRSUFFIX="64"
else
  SLKCFLAGS="-O2"
  LIBDIRSUFFIX=""
fi

set -e

MANVER="${MANVER:-4}"
MANUAL=designers_manual_${MANVER}.zip
TARVER="${VERSION//_/-}"
DOCDIR=/usr/doc/$PRGNAM-$VERSION
PKGDOC=$PKG/$DOCDIR

rm -rf $PKG
mkdir -p $TMP $PKG $OUTPUT
cd $TMP
rm -rf $PRGNAM-$TARVER
tar xvf $CWD/$PRGNAM-$TARVER.tar.gz
cd $PRGNAM-$TARVER
chown -R root:root .
find -L .  -perm /111 -a \! -perm 755 -a -exec chmod 755 {} \+ -o \
        \! -perm /111 -a \! -perm 644 -a -exec chmod 644 {} \+

# 20201104 bkw: Since inform is for game purposes, install the binary
# to /usr/games, and its man page to section 6. Also change the
# "see also" to a man page that actually might exist.
sed -e '1s/"1"/"6"/' -e "s,zcode-interpreter,frotz," $PRGNAM.1 > $PRGNAM.6

# 20201104 bkw: upstream now supports DESTDIR again.
# 20210831 bkw: upstream *broke* DESTDIR again. Sigh.
patch -p1 < $CWD/destdir.diff

# 20210831 bkw: install-strip failed with "text file busy" on -current,
# use -Wl,-s to build the binary stripped in the first place.

make install \
     OPTS="$SLKCFLAGS" \
     LDFLAGS="-Wl,-s" \
     BINDIR=/usr/games \
     MANDIR=/usr/man/man6 \
     MANPAGE=$PRGNAM.6 \
     PREFIX=/usr \
     TUTORDIR=$DOCDIR/tutor \
     DEMODIR=$DOCDIR/demos \
     DESTDIR=$PKG

gzip $PKG/usr/man/man?/*.?

# 20201104 bkw: the binary got moved, be kind and provide a symlink
mkdir -p $PKG/usr/bin
ln -s ../games/$PRGNAM $PKG/usr/bin/$PRGNAM

# Inform started life (and is still used primarily) on OSes
# with case-insensitive filenames, meaning that a lot of inform
# source refers to e.g. VerbLib or even VERBLIB (with or without
# the .h at the end, which could also be capitalized as .H). The
# Makefile can create a few symlinks to support this, but let's
# be more all-inclusive. We got 8 .h files, and end up with 70+
# symlinks. There's no kill like overkill!
( cd $PKG/usr/share/$PRGNAM/std/lib
  for file in *.h; do
    name=${file%%.*}
    #           Name.h     NAME.h
    for link in ${name^}.h ${name^^}.h; do
      ln -s $file $link
    done
  done

  # MixedCase isn't standardized, these are guesses. Some are pretty
  # unlikely, but they don't cost much. Better to have and not need,
  # than to need and not have.
  for link in VerbLib VerbLIB verbLib verbLIB \
              InFix InFIX inFix inFIX \
              InfGlk InfGLK infGlk infGLK \
              LinkLpa LinkLPA linkLpa linkLPA
  do
    ln -s ${link,,}.h $link.h
  done

  # Any of the above might appear with .H. Use readlink so all the
  # variant names link to the real file (no symlinks to symlinks).
  # This might be overcautious: the example code all just says
  # e.g. 'Include "Parser"', without the .h.
  for file in *.h; do
    dest="${file%%.*}.H"
    [ -L "$file" ] && file="$( readlink $file )"
    ln -s $file $dest
  done
)

# Upstream removed the manual for licensing reasons. It belongs in the
# binary package IMO.
mkdir -p $PKGDOC/manual
( cd $PKGDOC/manual
  unzip $CWD/$MANUAL
  # manual has no subdirs
  chmod 644 *
  chown root.root *
)

# Inform Beginners' Guide, required reading.
cat $CWD/IBG.pdf > $PKGDOC/inform_beginners_guide.pdf
ln -s inform_beginners_guide.pdf $PKGDOC/IBG.pdf

# Library documentation should be visibile in /usr/doc.
mkdir -p $PKGDOC/lib
for i in ChangeLog README.txt voices_and_tenses.txt; do
  ln -s ../../../share/$PRGNAM/std/lib/$i $PKGDOC/lib
done

# Same for punyinform docs.
mkdir -p $PKGDOC/punyinform
for i in README.md LICENSE documentation; do
  ln -s ../../../share/$PRGNAM/punyinform/$i $PKGDOC/punyinform
done

# ARTISTIC and COPYING are redundant: src/licence.txt includes the
# full text of both.
cp -a AUTHORS NEWS README* src/*.txt src/*.html docs/* $PKGDOC
cat $CWD/$PRGNAM.SlackBuild > $PKGDOC/$PRGNAM.SlackBuild

mkdir -p $PKG/install
cat $CWD/slack-desc > $PKG/install/slack-desc

cd $PKG
/sbin/makepkg -l y -c n $OUTPUT/$PRGNAM-$VERSION-$ARCH-$BUILD$TAG.$PKGTYPE
